import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraDatacenterTableApi } from '#/api/infra/db/table';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { getDataSourceConfigList } from '#/api/infra/db/data-source-config';
import { getDeptListCondition } from '#/api/system/dept';
import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'dataSourceConfigId',
      label: '数据源',
      component: 'ApiSelect',
      componentProps: {
        api: async () => {
          const data = await getDataSourceConfigList();
          return data.map((item) => ({
            label: item.name,
            value: item.id,
          }));
        },
        autoSelect: 'first',
        placeholder: '请选择数据源',
      },
      rules: 'selectRequired',
    },

    {
      fieldName: 'tableName',
      label: '数据表名',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入数据表名',
      },
    },
    {
      fieldName: 'tableDesc',
      label: '数据表描述',
      rules: 'required',

      component: 'Input',
      componentProps: {
        placeholder: '请输入数据表描述',
      },
    },
    {
      fieldName: 'actualTable',
      label: '物理表名',
      component: 'Input',
      rules: 'required',

      componentProps: {
        placeholder: '请输入物理表名',
      },
    },
    {
      fieldName: 'status',
      label: '数据状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'deptId',
      label: '所属组织',
      component: 'ApiTreeSelect',
      disabled: false,
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择所属组织',
        treeDefaultExpandAll: true,
        autoSelect: 'first',
      },
    },

    {
      fieldName: 'options',
      label: '扩展项',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入扩展项',
      },
    },
  ];
}
/** 导入数据库表的表单 */
export function useImportTableFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'dataSourceConfigId',
      label: '数据源',
      component: 'ApiSelect',
      componentProps: {
        api: async () => {
          const data = await getDataSourceConfigList();
          return data.map((item) => ({
            label: item.name,
            value: item.id,
          }));
        },
        autoSelect: 'first',
        placeholder: '请选择数据源',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'name',
      label: '表名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入表名称',
      },
    },
    {
      fieldName: 'comment',
      label: '表描述',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入表描述',
      },
    },
  ];
}
/** 导入数据库表表格列定义 */
export function useImportTableColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    { field: 'name', title: '表名称', minWidth: 200 },
    { field: 'comment', title: '表描述', minWidth: 200 },
  ];
}
/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '数据源',
      field: 'dataSourceConfigId',
    },
    {
      label: '数据表名',
      field: 'tableName',
    },
    {
      label: '数据表描述',
      field: 'tableDesc',
    },
    {
      label: '物理表名',
      field: 'actualTable',
    },
    {
      label: '数据状态',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        });
      },
    },
    {
      label: '扩展项',
      field: 'options',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '组织名称',
      field: 'deptName',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'dataSourceConfigId',
      label: '数据源',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getDataSourceConfigList();
          return data.map((item) => ({
            label: item.name,
            value: item.id,
          }));
        },
        autoSelect: 'first',
        placeholder: '请选择数据源',
      },
    },
    {
      fieldName: 'tableName',
      label: '数据表名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入数据表名',
      },
    },
    {
      fieldName: 'tableDesc',
      label: '数据表描述',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入数据表描述',
      },
    },
    {
      fieldName: 'actualTable',
      label: '物理表名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入物理表名',
      },
    },
    {
      fieldName: 'deptId',
      label: '组织ID',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择所属组织',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'status',
      label: '数据状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择数据状态',
      },
    },

    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<InfraDatacenterTableApi.DatacenterTable>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'dataSourceConfigId',
      title: '数据源',
      minWidth: 120,
    },
    {
      field: 'tableName',
      title: '数据表名',
      minWidth: 120,
    },
    {
      field: 'tableDesc',
      title: '数据表描述',
      minWidth: 120,
    },
    {
      field: 'actualTable',
      title: '物理表名',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '数据状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'deptName',
      title: '组织名称',
      minWidth: 200,
    },
    {
      field: 'options',
      title: '扩展项',
      minWidth: 200,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
